<template>
	<view class="content">
		<view class="head">
			<image v-if="avatar" :src="avatar"></image>

			<image v-else src="http://sni1muz85.hd-bkt.clouddn.com/img/my/avatar.jpeg"></image>
		</view>

		<view class="list">
			<view class="item">
				<view class="title">修改头像</view>
				<view class="right">
					<button class="avatar" type="default" plain="true" style="border:none;" open-type="chooseAvatar"
						@chooseavatar="onChooseAvatar">
						选择
					</button>
					<!--                    <text v-if="!info.avatar">请选择</text>-->
					<!--                    <text v-else>选择</text>-->
					<image src="http://sni1muz85.hd-bkt.clouddn.com/img/icons/arrow_right.png"></image>
				</view>
			</view>

			<view class="item">
				<view class="title">昵称</view>
				<view class="right">
					<!--              <button v-if="name" class="avatar-wrapper" open-type="nickname" @chooseavatar="onChooseName">-->
					<!--                修改-->
					<!--              </button>-->
					<!--              <button v-else class="avatar-wrapper" open-type="nickname" @chooseavatar="onChooseName">-->
					<!--                {{ name }}-->
					<!--              </button>-->
					<input class="inputName" style="text-align:right" :bindblur="onChooseName" type="nickname"
						v-model="name" placeholder="请输入昵称" />
					<!--              <text v-if="!info.name ">修改</text>-->
					<!--              <text v-else>{{ info.name }}</text>-->
					<image src="http://sni1muz85.hd-bkt.clouddn.com/img/icons/arrow_right.png"></image>
				</view>
			</view>

			<view class="item" @click="editPhone">
				<view class="title">手机号</view>
				<view class="right">
					<!--              <text v-if="!mobile "></text>-->
					<text>{{ mobile?mobile:""}}</text>
					<image src="http://sni1muz85.hd-bkt.clouddn.com/img/icons/arrow_right.png"></image>
				</view>
			</view>

			<!-- <view class="item" @click="editAccount" >
            <view class="title">提现账户</view>
            <view class="right"> -->
			<!--              <text >"withdrawAccount ">修改</text>-->
			<!--  <text >{{withdrawAccount?withdrawAccount:"" }}</text>
              <image src="http://sni1muz85.hd-bkt.clouddn.com/img/icons/arrow_right.png"></image>
            </view>
          </view> -->
		</view>

		<!-- 占位 -->
		<view class="btn" @click="saveInfo">保存</view>

		<!--&lt;!&ndash;      <page-meta ></page-meta>&ndash;&gt;-->
		<!--        <uni-popup :page-style="'overflow:'+(show?'hidden':'visible')" class="personalInformation-popup" ref="picturePopup" type="bottom"-->
		<!--                   mask-background-color="rgba(0, 0, 0, 0.6)"-->
		<!--                   @change="change"-->
		<!--        >-->
		<!--          <view class="personalInformation-content">-->
		<!--            <view class="picturePopup-list">-->
		<!--              <view class="picturePopup-item" v-for="(item, index) in sourceType" :key="item.title"-->
		<!--                    @click="pictureClick(item, index)">-->
		<!--                {{ item.title }}-->
		<!--              </view>-->
		<!--            </view>-->
		<!--          </view>-->
		<!--        </uni-popup>-->
	</view>

</template>

<script>
	export default {


		data() {
			return {
				dataList: [{
						title: "修改头像",
						value: "",
					},
					{
						title: "昵称",
						value: "张三",
					},
					{
						title: "加微",
						value: "adasd1111",
					},
					{
						title: "手机号",
						value: "136****0777",
					},
					{
						title: "提现账户",
						value: "",
					},
				],
				info: {},
				show: false,
				sourceType: [{
						type: 'camera',
						title: '拍照',
						// name: that.$t('addReminder.TakePictures')
					},
					{
						type: 'album',
						title: '相册',
						// name: that.$t('addReminder.PhotoAlbum')
					},
					{
						type: '',
						title: '取消',
						// name: that.$t('login.Cancel')
					},
				],
				avatar: "",
				name: "",
				mobile: "",
				withdrawAccount: ""
			}
		},
		onLoad() {
			// 获取屏幕导航栏高度
			let value = uni.getStorageSync('navHeight')
			this.navHeight = value.height
			this.initData()
		},

		methods: {
			onChooseAvatar(e) {
				console.log(e)
				let that = this;
				const {
					avatarUrl
				} = e.detail
				// this.avatar = avatarUrl
				uni.uploadFile({
					url: that.$api.uploadImg, //仅为示例，非真实的接口地址
					filePath: avatarUrl,
					name: 'file',
					success: (res) => {
						console.log(res);
						let da = JSON.parse(res.data);
						that.avatar = da.url
					}
				});

			},
			onChooseName(e) {
				console.log(e)

				const {
					avatarUrl
				} = e.detail

			},
			editName() {
				let that = this
				uni.showModal({
					title: '修改名称',
					editable: true,
					content: this.info.name,
					success: function(res) {
						console.log(res)
						if (res.confirm) {
							// that.updateInfo({name:res.content})
							that.info.name = res.content
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				});
			},
			change(e) {
				this.show = e.show
			},
			pictureClick(item, index) {
				this.$refs.picturePopup.close()
				if (item && item.type) {
					this.chooseImage(item)
				}
			},
			chooseImage(obj) {
				let that = this
				let sourceType = ['camera']
				if (obj && obj.type == 'album') {
					sourceType = ['album']
				}
				uni.chooseImage({
					count: 1,
					sizeType: ['original'],
					sourceType: sourceType,
					success: (res) => {
						if (res && res.tempFilePaths && res.tempFilePaths.length) {
							that.uploadFile(res)
						}
					}
				})
			},

			saveInfo() {
				// let that=this
				console.log(this.info)

				this.updateInfo()


			},
			choseImage() {
				// this.$refs.picturePopup.open()
				let that = this
				uni.chooseImage({
					count: 1,
					sizeType: ['original'],
					sourceType: ['album'],
					success: (res) => {
						uni.uploadFile({
							url: that.$api.uploadImg, //仅为示例，非真实的接口地址
							filePath: res.tempFilePaths[0],
							name: 'file',
							success: (res) => {
								console.log(res);
								let da = JSON.parse(res.data);
								that.info.avatar = da.url
							}
						});
					}
				})


			},
			validatePhoneNumber(phoneNumber) {
				if (phoneNumber.length !== 11) {
					return false;
				}
				var reg = /^1\d{10}$/;
				return reg.test(phoneNumber);
			},
			editPhone() {
				console.log(123123)
				let that = this
				let mobile = that.mobile ? that.mobile : "输入"
				wx.showModal({
					title: '修改',
					editable: true,
					content: mobile,
					success: function(res) {
						console.log(res)
						if (res.confirm) {
							if (that.validatePhoneNumber(res.content)) {
								that.mobile = res.content

							} else {
								wx.showToast({
									title: '手机号格式错误',
									icon: 'error',
									duration: 1000
								})

							}
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				});
			},


			editAccount() {
				let that = this
				let withdrawAccount = that.withdrawAccount ? that.withdrawAccount : ""

				wx.showModal({
					title: '修改',
					editable: true,
					content: withdrawAccount,
					success: function(res) {
						console.log(res)
						if (res.confirm) {
							that.withdrawAccount = res.content
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				});
			},

			updateInfo() {
				if (this.name.length > 16) {
					wx.showToast({
						title: '名称过长',
						icon: 'error',
						duration: 1000
					})
					return
				}
				let obj = {
					name: this.name,
					avatar: this.avatar,
					withdrawAccount: this.withdrawAccount,
					mobile: this.mobile
				}

				this.$api.request({
					url: this.$api.updateUser,
					method: "POST",
					data: obj
				}).then(res => {
					console.log(res)
					// this.info=res.data
					uni.navigateBack()
					uni.showToast({
						title: "成功",
						duration: 1000
					})

				})
			},
			initData() {
				this.$api.request({
					url: this.$api.getInfo,
				}).then(res => {
					console.log(res)
					this.info = res.data
					this.avatar = res.data.avatar
					this.name = res.data.name
					this.mobile = res.data.mobile
					this.withdrawAccount = res.data.withdrawAccount
				})
			},
		}
	}
</script>

<style lang="less" scoped>
	@import "@/static/css/app.less";

	.content {
		display: flex;
		flex-direction: column;
		align-items: center;
		max-height: 100vh;
		background-color: #fff;
		min-height: 100vh;

		.head {
			width: 710rpx;
			display: flex;
			justify-content: center;
			margin: 20rpx 0;

			image {
				height: 100rpx;
				width: 100rpx;
				border-radius: @radius;
			}
		}

		.avatar {
			border: none;
		}

		.list {
			width: 631rpx;

			.item {
				display: flex;
				justify-content: space-between;
				align-items: center;
				font-size: 32rpx;
				margin: 40rpx;
				padding-bottom: 20rpx;
				border-bottom: 1rpx solid #c7c7c7;

				.title {
					color: #111111;
				}

				.right {
					display: flex;
					align-items: center;

					text {
						color: #8A8A8A;
						margin-right: 20rpx;
					}

					image {
						height: 30rpx;
						width: 30rpx;
					}

					.inputName {
						//margin-right: -110rpx;
						padding-right: 20rpx;
					}
				}
			}
		}

		.btn {
			font-size: 32rpx;
			font-weight: bold;
			color: #fff;
			background-color: #07C160;
			border-radius: 50rpx;
			padding: 20rpx 150rpx;
			margin: 80rpx 0 0;
		}
	}

	.picturePopup {
		&-list {
			width: 100%;
			overflow: hidden;
			background-color: #f8f8f8;
		}

		&-item {
			width: 100%;
			overflow: hidden;
			background-color: #fff;
			margin-bottom: 10rpx;
			height: 110rpx;
			line-height: 110rpx;
			text-align: center;
			font-size: 32rpx;
			font-weight: 400;
			color: #333;
			font-family: 'PingFangSC';
		}
	}
</style>